{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n获取券商股的A+H历史K线\\n主要用于分析AH折溢价情况\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from futuquant import *\n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "'''\n",
    "获取券商股的A+H历史K线\n",
    "主要用于分析AH折溢价情况\n",
    "'''\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "quote_ctx = OpenQuoteContext(host='127.0.0.1',port=11111)\n",
    "(ret1, dfH) = quote_ctx.get_history_kline('HK.06837',start='2006-01-01',end='2018-06-30')\n",
    "(ret2, dfA) = quote_ctx.get_history_kline('SH.600837',start='2006-01-01',end='2018-06-30')\n",
    "if ret1 == RET_OK:\n",
    "    dfH.to_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\hk_06837.csv\")\n",
    "else:\n",
    "    print(\"get hisdata failed error reason:%r\"%dfH)\n",
    "    \n",
    "if ret2 == RET_OK:\n",
    "    dfA.to_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\SH_600837.csv\")\n",
    "else:\n",
    "    print(\"get hisdata failed error reason:%r\"%dfA)\n",
    "quote_ctx.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print (dfA.head(5))\n",
    "dfH = pd.read_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\hk_06837.csv\", index_col=\"time_key\")\n",
    "dfH = dfH[[\"code\",\"close\",\"pe_ratio\"]]\n",
    "dfA = pd.read_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\SH_600837.csv\",index_col=\"time_key\")\n",
    "dfA = dfA[[\"code\",\"close\",\"pe_ratio\"]]\n",
    "dfA = dfA.rename(columns={\"close\":\"Aclose\",\"pe_ratio\":\"Ape_ratio\"})\n",
    "dfAH = pd.merge(dfH, dfA, left_index=True,right_index=True)\n",
    "dfAH.to_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\AH_600837.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfAH[\"premium\"] = (dfAH[\"Aclose\"] - dfAH[\"close\"])/ dfAH[\"Aclose\"]\n",
    "print(dfAH[\"premium\"].describe())\n",
    "dfAH.to_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\AH_600837.csv\")\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfH = pd.read_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\hk_06837.csv\", index_col=\"time_key\")\n",
    "print(dfH.index[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(datetime.strptime(dfH.index[-1][0:9],\"%Y-%m-%d\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str1=\"hk_06837\"\n",
    "str1[3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "quote_ctx = OpenQuoteContext(host='127.0.0.1',port=11111)\n",
    "(ret,df) = quote_ctx.get_plate_list(Market.SH,Plate.ALL)\n",
    "#print(df)\n",
    "df.to_csv(\"C:\\\\quanttime\\\\data\\\\tmp\\\\futu_plate_info.csv\",mode='a')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "quote_ctx.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-07-12 12:00:32,086 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:00:32,945 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:00:35,525 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:00:36,185 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:00:36,865 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:00:39,905 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:00:40,085 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:00:40,946 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:00:43,526 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:00:44,187 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:00:44,867 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:00:47,908 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:00:48,087 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:00:48,947 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:00:51,527 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:00:52,187 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:00:52,867 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:00:55,907 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:00:56,087 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:00:56,947 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:00:59,528 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:01:00,189 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:01:00,869 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:01:03,909 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:01:04,089 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:01:04,949 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:01:07,529 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:01:08,190 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:01:08,869 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:01:11,909 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:01:12,089 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:01:12,949 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:01:15,530 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:01:16,190 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:01:16,870 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:01:19,912 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:01:20,091 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:01:20,951 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:01:23,531 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n",
      "2018-07-12 12:01:24,191 [open_context_base.py] on_activate:345: Keepalive: conn_id=7;\n",
      "2018-07-12 12:01:24,871 [open_context_base.py] on_activate:345: Keepalive: conn_id=5;\n",
      "2018-07-12 12:01:27,912 [open_context_base.py] on_activate:345: Keepalive: conn_id=8;\n",
      "2018-07-12 12:01:28,092 [open_context_base.py] on_activate:345: Keepalive: conn_id=6;\n",
      "2018-07-12 12:01:28,952 [open_context_base.py] on_activate:345: Keepalive: conn_id=3;\n",
      "2018-07-12 12:01:31,532 [open_context_base.py] on_activate:345: Keepalive: conn_id=1;\n"
     ]
    }
   ],
   "source": [
    "(ret,df) = quote_ctx.get_plate_stock(\"SH.BK0047\")\n",
    "print (df[[\"code\",\"stock_name\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-07-12'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.today().date().strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
